<?php

namespace application\diyform\controller;

use application\admin\controller\base as auth;
use ticky\request;
use ticky\response;
use ticky\schema;

//diyform/diyform/manage

class diyform_info extends auth {

    private $modelid, $modeltable, $modelname;

    public function __construct() {
        parent::__construct();
        $this->modelid = isset($_GET['modelid']) ? intval($_GET['modelid']) : (isset($_POST['modelid']) ? intval($_POST['modelid']) : 0);
        $this->public_set_modelinfo();
    }

    /**
     * 用户提交表单信息列表
     */
    public function index() {
        $search = [];
        $ret = db($this->modeltable)->where($search)->order('id desc')->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->assign('modelid', $this->modelid);
        $this->display('diyform_info_list');
    }

    /**
     * 查看
     */
    public function view() {

    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', 'del');
        $ids = request::post('ids', []);
        if ($optype == "del") {
            for ($i = 0; $i < count($ids); $i++) {
                $id = $ids[$i];
                db($this->modeltable)->delete(['id' => $id]);
            }
            showmsg('删除成功', '/diyform/diyform_info');
        }
    }

    /**
     * 获取模型信息
     */
    private function public_set_modelinfo() {
        $data = db('model')->field('name,tablename')->where(array('modelid' => $this->modelid))->find();
        if ($data) {
            $this->modelname = $data['name'];
            $this->modeltable = $data['tablename'];
        } else {
            showmsg('模型不存在！', 'stop');
        }
    }

}
